﻿using System;
using System.Activities;

namespace NMemcached.Client.Activities
{
    /// <summary>
    /// Provides an abstract base class for Memcached activities
    /// </summary>
    public abstract class MemcachedActivity : CodeActivity
    {
        /// <summary>
        /// Gets or sets the connection used to issue commands
        /// </summary>
        public InArgument<MemcachedConnection> Connection { get; set; }

        /// <summary>
        /// Gets or sets the connection for use by inherited classes
        /// </summary>
        protected MemcachedConnection CacheConnection { get; set; }

        /// <summary>
        /// Initializes a new instance of the MemcachedActivity class
        /// </summary>
        protected MemcachedActivity()
            : base()
        {
        }

        /// <summary>
        /// Performs the execution of the activity
        /// </summary>
        /// <param name="context">The execution context under which the activity executes</param>
        protected override void Execute(CodeActivityContext context)
        {
            MemcachedConnection connection = this.Connection.Get(context);
            if (connection == null)
            {
                throw new ArgumentNullException("Connection");
            }
            else
            {
                this.CacheConnection = this.Connection.Get(context);
            }
        }
    }
}